var orderParams = {
  time: 60,
  bol: true,
  phone: '',
  code: '',
  name: '',
  city: '',
  date: '',
  note: '活动号：' + activityNo + '，photoplus试用申请咨询'
}

orderInit();
function orderInit() {
  // 预约
  $('#order').click(goOrder);
  $('#order-button').click(getOrderCode);
  $('#submit-order').click(orderSubmit);
}

// 预约弹窗
function goOrder() {
  $('.order-wrapper').fadeIn();
  $('body').css('position', 'fixed'); // 弹窗输入框光标移动解决方法
}

function getOrderCode() {
  orderParams.phone = $('#order-phone').val();

  var isPhone = checkPhone(orderParams.phone);

  if (!isPhone) {
    toast('请填写正确的手机号');
    return;
  }
  if (orderParams.bol) {
    orderParams.time = 60;
    orderParams.bol = false;
    timerStatus();
    timer();

    $.ajax({
      url: '/home/code/send',
      type: 'post',
      dataType: 'json',
      data: {
        phone: orderParams.phone,
      },
      success: function(res) {
        if (res.success) {
          toast('验证码已发送');
        } else {
          toast('发送失败，稍后重试');
        }
      }
    })
  }
}

function orderSubmit() {
  var submitActive = $('#submit-order').hasClass('active');
  if (!submitActive) {

    orderParams.code = $('#order-code').val();
    orderParams.phone = $('#order-phone').val();
    orderParams.name = $('#order-name').val();
    // orderParams.date = Date.parse(new Date($('#date').val()));

    var isPhone = checkPhone(orderParams.phone);
    if (!orderParams.name) {
      toast('请输入您的姓名');
      return;
    }
    if (!isPhone) {
      toast('请填写正确的手机号');
      return;
    }
    $('#submit-order').addClass('active');
    $('.submit-loading').removeClass('none');
    $.ajax({
      url: 'order/add',
      type: 'post',
      dataType: 'json',
      data: {
        activityNo: activityNo,
        phone: orderParams.phone,
        verifyCode: orderParams.code,
        name: orderParams.name,
        content: ''
      },
      success: function(res) {
        if (res.success) {
          toast('预约成功');
          $('.order-wrapper').fadeOut();
          $('#order-code, #order-phone, #order-name').val('');
        } else {
          toast(res.message);
        }
        $('#submit-order').removeClass('active');
        $('.submit-loading').addClass('none');
      }
    })
  }
}

function timerStatus() {
  if (orderParams.bol) {
    $('#order-button').removeClass('active');
    $('#order-button').html('获取验证码');
  } else {
    $('#order-button').addClass('active');
  }
}

function timer() {
  orderParams.time--;
  $('#order-button').html(orderParams.time + 's');
  if (orderParams.time !== 0) {
    setTimeout(timer, 1000);
  } else {
    orderParams.bol = true;
    timerStatus();
  }
}
